<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php 
	include_once('database.php');
	
  	$genre = $db -> query("SELECT * FROM ".$_GET["table"]." WHERE ".$_GET["column"]." = ".$_GET["id"].";");
	
	$rows = $genre ->num_rows;
	if($rows >0){

?>
	<center>
        <h3>
            Det vil påvirke:	
        </h3>
    </center>
    <table border="1" width="500px">
		<tr> 
			<td>
				Billede
            </td>
            <td>
                Navn
            </td>
            <td>
           		Medie
			</td>
         	<td>
             	Genre
       		</td>
           	<td>
            	Release dag
         	</td>
      	</tr>

	<?php     
        $materials = "";

        $counter = 1;
        
        while($stuff = $genre -> fetch_assoc()){
            $materials .= $stuff["MaterialId"];
            if($counter < $rows){
                $materials .= ', ';
            }
            $counter ++;
            
        }
        
        $query = "SELECT * FROM item WHERE MaterialId in(".$materials.") LIMIT ".$_GET["limit"]." OFFSET ".$_GET["offset"].";";
        $items = $db -> query($query);   
        while($result=$items -> fetch_assoc()){
			$pictureselect= $db -> query("SELECT *
										FROM itemimage
                                      	WHERE MaterialId = '".$result["MaterialId"]."';");

            $picture =  $pictureselect -> fetch_assoc();
                    
            $img = empty($picture["file_data"])? $picture["filename"]:"material_image.php?id=".$result["MaterialId"];
                
            $mediaselect = $db -> query("SELECT * 
                                        FROM media 
                                        WHERE MediaId =".$result["MediaId"].";");
                                        
            $genreselect = $db -> query("SELECT genre.GenreName
                                        FROM itemgenre, genre
                                        WHERE itemgenre.MaterialId =".$result["MaterialId"]."
                                        AND itemgenre.GenreId = genre.GenreId;");
                                        
            echo'<tr>
                    <td width="60px" height="80px">
                        <img src="'.$img.'" width="60px" />
                    </td>
                    <td>
                        '.htmlspecialchars($result["Name"]).'
                    </td>			
                    <td>';
                        if(($mediaselect -> num_rows) == 0){
                            echo 	'<a href="update_material.php?id='.$result["MaterialId"].'">
                                        Materialet
                                        <br>
                                        mangler medie
                                    </a>';
                        }else{
                            $media= $mediaselect -> fetch_assoc();
                                echo htmlspecialchars($media["MediaName"]);	
                        }
            echo	'</td>
                    <td>';
                        if(($number= $genreselect -> num_rows) == 0){
                            echo 	'<a href="update_material.php?id='.$result["MaterialId"].'">
                                        Materialet
                                        <br>
                                        mangler genre
                                    </a>';
                        }else{
                        
                             
                            $counter = 1;
                            while($genres = $genreselect -> fetch_assoc()){
                                echo htmlspecialchars($genres["GenreName"]);
                                if($counter < $number){
                                    echo ', ';
                                }
                                $counter ++;
    
                            }
                        }
            echo'	</td>
                    <td>
                        '. htmlspecialchars($result["ReleaseDate"]).'
                    </td>
                </tr>';	
        }
    ?>

    </table>
    
    <?php
	}else{
		echo 'Ingen materialer vil blive påvirket';	
		
	}
	?>
<input type="hidden" id="num_rows" value="<?php echo $rows ?>" />
</body>
</html>